<?php
/*******************************************************************************

    FinalsClub.org is a platform for students and faculty to collaborate
    and share their knowledge with the world.

    Copyright (C) 2009  Andrew Magliozzi
 
    This file is a part of FinalsClub.org.

    FinalsClub.org is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
    To contact FinalsClub.org, send an email to info@finalsclub.org, or
    send mail to Finals Club, c/o Veritas Tutors, 1132 Massachusetts Avenue,
    Cambridge, MA 02138.

*******************************************************************************/

class File extends BaseFile
{
	const TYPE = 'file';
	
	/**
	 * Return an authenticated URL for logging in to the Annotate server and viewing the file this File references
	 */
	public function getAnnotateFileViewURL() {
		$user = UserPeer::retrieveById($this->getOwnerId());
		$annotate_api = new AnnotateApi();
		$url = $annotate_api->getLoginDisplayFileURL($user->getEmail(), $this->getAnnotateDate(), $this->getCode());
		return $url;
	}

	/**
	 * Return a url on the annotate server that downloads the file
	 */
	public function getAnnotateFileDownloadURL() {
		$annotate_api = new AnnotateApi();
		$annotate_url = $annotate_api->getBaseURL();
		$url = $annotate_url.'/fc/download.php?d='.$this->getAnnotateDate().'&c='.$this->getCode().'&f='.urlencode($this->getName());
		return $url;
	}
	
	//get the url to show the thumbnail of this file type
	public function getAnnotateThumbnailURL()
	{	
		
		if(!$this->getThumbnail())
		{
			return NULL;
		}
		
		$annotate_api = new AnnotateApi();
		$annotate_url = $annotate_api->getBaseURL();
		
		//'http://annotate-stage.finalsclub.org/docs/2009-03-23/Kg351yok/_thumb100.png'
		$url = $annotate_url.'/docs/'.$this->getAnnotateDate().'/'.$this->getCode().'/'.$this->getThumbnail();
		
		return $url;
	}

	public function save(PropelPDO $con = null) {
		
		$activity_feed = new ActivityFeed();
		
		$activity_feed->setUserId($this->getOwnerId());
		$activity_feed->setType(ActivityFeedPeer::FILE_TYPE);
		
		if($this->getGroupId())
		{
			$activity_feed->setGroupId($this->getGroupId());
		
			$group = GroupPeer::retrieveById($this->getGroupId());
			// check if the group is public, if it is the feed should be as well
			if($group->getVisibility() == GroupPeer::EVERYONE)
				$activity_feed->setIspublic(true);
			else
				$activity_feed->setIspublic(false);
		}
		
		
		// is this a new task?
		if($this->isNew())
			$action = "uploaded";
		
		$message = "<strong>" . $action . " a file</strong>";	// the rest of the message will be constructed by the RenderFeeds helper
		$activity_feed->setMessage($message);
		
		parent::save($con);		// we need to save the file first
		$this->updateLuceneIndex();  // update Lucene index
		
		$activity_feed->setTypeId($this->getId());
		$activity_feed->save();
	
	}

	public function delete(PropelPDO $con = null)
	{
		$index = Search::getLuceneIndex(Search::FILE);
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);
		if ($hit) {
  			foreach($hit as $h) { $index->delete($h->id); }
		}

		parent::delete($con);
	}

	public function updateLuceneIndex() {
  		$index = Search::getLuceneIndex(Search::FILE);
 
  		// remove an existing entry
		$term  = new Zend_Search_Lucene_Index_Term($this->getId(), 'pk');
		$query = new Zend_Search_Lucene_Search_Query_Term($term);
		$hit   = $index->find($query);
  		if ($hit) {
    			foreach($hit as $h) { $index->delete($h->id); }
  		}
 
  		$doc = new Zend_Search_Lucene_Document();
 
	  	// store primary key to identify it in the search results
	  	$doc->addField(Zend_Search_Lucene_Field::Keyword('pk', $this->getId()));
 
	  	// index fields
	  	$doc->addField(Zend_Search_Lucene_Field::Text('name', $this->getName()));
		$doc->addField(Zend_Search_Lucene_Field::Text('description', $this->getDescription()));
		$doc->addField(Zend_Search_Lucene_Field::UnIndexed('created',$this->getCreatedAt()));
		$doc->addField(Zend_Search_Lucene_Field::UnStored('userid', $this->getOwnerId()));
		$doc->addField(Zend_Search_Lucene_Field::Text('tags', $this->getTags()));

  		// add to the index
  		$index->addDocument($doc);
  		$index->commit();
  	}
  	
  	//use this owner id to return the object
  	public function getOwner()
  	{
  		return UserPeer::retrieveById($this->getOwnerId());
  	}
}
